const express = require('express');
const multer = require('multer');
const pdfParse = require('pdf-parse');
const mammoth = require('mammoth');
const textract = require('textract');
const d3 = require('d3-dsv'); // 引入 d3-dsv
const csvParser = require('csv-parser');
const fs = require('fs');
const path = require('path');
const xlsx = require('xlsx');

const app = express();
// const upload = multer({dest: 'uploads/'}); // 文件上传到uploads目录
const uploadDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadDir)) {
    fs.mkdirSync(uploadDir);
}
const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, uploadDir); // 文件存储目录
    },
    // filename: (req, file, cb) => {
    //     // 生成唯一的文件名：时间戳 + 随机数 + 文件扩展名
    //     const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
    //     const ext = path.extname(file.originalname); // 获取文件扩展名
    //     cb(null, file.fieldname + '-' + uniqueSuffix + ext);
    // },
    fileFilter: (req, file, cb) => {
        // 处理文件名编码
        file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf8')
        console.log("888")
        console.log(file.originalname)
        cb(null, true);
    },
    filename: (req, file, cb) => {
        // 生成唯一的文件名：时间戳 + 随机数 + 文件扩展名
        const ext = path.extname(file.originalname); // 获取文件扩展名
        const parts = Buffer.from(file.originalname, 'latin1').toString('utf8').split('.');
        const result = parts[0];
        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
        cb(null, result + '-' + uniqueSuffix + ext);
        // cb(null, Buffer.from(file.originalname, 'latin1').toString('utf8'));
    },
});
// 初始化上传中间件
const upload = multer({storage});
// 上传PDF文件的接口
app.post('/upload', upload.single('file'), async (req, res) => {
    try {
        if (!req.file) {
            return res.status(400).send('请上传PDF文件');
        }
        console.log("6666")
        console.log(req.file)
        console.log(req.file.mimetype)
        const filePath = req.file.path;
        // const fileName = req.file.originalname;
        const fileName = req.file.filename;
        console.log("filePath")
        console.log(filePath)
        if (req.file.mimetype === 'application/pdf') {
            // 解析PDF文件
            const dataBuffer = fs.readFileSync(req.file.path);
            const data = await pdfParse(dataBuffer);

            // 提取文本内容
            const textContent = data.text;
            console.log('提取的文本内容：', textContent);

            // 返回结果
            res.status(200).json({
                message: 'PDF解析成功并发送到模型',
                metadata: {
                    source: fileName
                },
                pageContent: textContent
            });
        }
        if (req.file.mimetype === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
            const result = await mammoth.extractRawText({path: filePath});
            // 提取文本内容
            const textContent = result.value;
            console.log('提取的文本内容：', textContent);
            // 返回结果
            res.status(200).json({
                message: 'word解析成功并发送到模型',
                metadata: {
                    source: fileName
                },
                type: req.file.mimetype,
                pageContent: textContent
            });
        }
        if (req.file.mimetype === 'text/csv') {
            const csvData = require('fs').readFileSync(filePath, 'utf8');
            console.log('csvData')
            console.log(csvData)
            console.log(csvData.trim())
            // 使用 d3-dsv 解析 CSV 数据
            // const parsed = d3.parseRows(csvData.trim())
            let parsed = d3.csvParseRows(csvData.trim())
            const headers = parsed[0]
            parsed = parsed.slice(1)

            const parsedData = parsed.map((row) =>
                row.map((value, index) => `${headers[index]}: ${value}`).join("\n")
            )
            console.log("parsedData");
            console.log(parsedData);
            res.status(200).json({
                message: 'word解析成功并发送到模型',
                metadata: {
                    source: fileName
                },
                type: req.file.mimetype,
                pageContent: parsedData
            });

        }
        // 删除上传的文件
        // fs.unlinkSync(filePath);
    } catch (error) {
        console.error('处理PDF时出错：', error);
        res.status(500).send('服务器内部错误');
    }
});

// 启动服务器
const PORT = 3000;
app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
});